<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="快递单号" prop="express_number">
				<el-input ref="express" v-model="form.express_number" placeholder="请输入快递单号"  clearable></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="email">
				<el-select ref="selectCity" v-model="form.type" class="m-2" placeholder="选择状态" size="large">
					<el-option label="极速退款登记" value="1"></el-option>
					<el-option label="极速退款收到" value="2"></el-option>
					<el-option label="拦截登记" value="3"></el-option>
					<el-option label="拦截收到" value="4"></el-option>
					<el-option label="取消寄件登记" value="5"></el-option>
					<el-option label="取消寄件收到" value="6"></el-option>
					<el-option label="退件收到" value="7"></el-option>
					<el-option label="作废" value="8"></el-option>
					<el-option label="丢件登记" value="9"></el-option>
					<el-option label="买家问题件" value="999"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="金额" prop="amount">
				<el-input ref="express" type="number" v-model="form.amount" placeholder="请输入金额"  clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="express_number">
				<el-input ref="express" v-model="form.remake" placeholder="请输入备注"  clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

	export default {
		emits: ['success', 'closed'],
		
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
				},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入姓名'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit(){
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if(!valid){
					return false
				}
				this.isSaveing = true;
				var res
				if (this.mode=='add') {
					res = await this.$API.refund.addExpress.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {

						//修改 search_url 修改话术 script
						if (this.form.type == 3 || this.form.type == 4) {
							this.form.script = this.form.express_number + ' 拦截'
						} else if (this.form.type == 5 || this.form.type == 6) {
							this.form.script = this.form.express_number + ' 取消寄件'
						}

						if (this.form.express_number.indexOf("JT") != -1) {
							this.form.search_url = "https://www.baidu.com/s?ie=UTF-8&wd=" + this.form.express_number + '极兔'
						} else {
							this.form.search_url = "https://www.baidu.com/s?ie=UTF-8&wd=" + this.form.express_number
						}

						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					res = await this.$API.refund.editExpress.post(this.form);
					this.isSaveing = false;

					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("修改成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}

				}
			},
			//表单注入数据
			setData(data){

				
				data.type = data.type.toString();

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.form, data)

			}
		}
	}
</script>

<style>
</style>
